﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Outlook.WorkItems.Extension.Controls
{
    /// <summary>
    /// Extensions methods for Treeview Control
    /// </summary>
    public static class TreeNodeExtension
    {
        /// <summary>
        /// Find all nodes that verify a predicat
        /// </summary>
        /// <param name="nodesCollection"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public static IEnumerable<TreeNode> FindNodes(this TreeNodeCollection nodesCollection, Func<TreeNode, bool> predicate)
        {
            var nodes = nodesCollection.Cast<TreeNode>();
            return nodes.SelectMany(n => FindNodes(n.Nodes, predicate)
                        .Union(nodes.Where(n2 => predicate(n2))));
        }
    }
}
